package edu.ustb.efws.pipeline.collect.handler.meter;

import java.time.Instant;
import java.util.StringJoiner;

/**
 * 测量点
 *
 * @author TS Group
 */
public class MeterPoint {

    /**
     * position编号
     */
    private final int positionId;

    /**
     * 测量windowTime中序号 System.currentTimeMillis()/windowTime
     */
    private final int index;

    /**
     * 开始分数
     */
    private float startScore = 0f;

    /**
     * 结束分数
     */
    private float endScore = 0f;

    /**
     * 最大分数
     */
    private float maxScore = 0f;

    /**
     * 最小分数
     */
    private float minScore = Float.MAX_VALUE;

    /**
     * 总分数
     */
    private float totalScore = 0f;

    /**
     * 总次数
     */
    private int count  = 1;

    /**
     * 修改时间戳
     */
    private volatile long updateTime;

    /**
     * 存储时间戳
     */
    private volatile long repoTime;


    /**
     * 构造{@link MeterPoint}
     *
     * @param positionId 测量点编号
     * @param index     时间窗序号
     * @param score 分数
     */
    public MeterPoint(int positionId, int index, float score) {
        this.positionId = positionId;
        this.startScore = score;
        this.maxScore = score;
        this.minScore = score;
        this.index = index;
        this.updateTime = System.currentTimeMillis();
        //保证创建时可以刷新
        this.repoTime = updateTime - 1000L;
    }

    public void incScore(float score) {
        Instant instant = Instant.now();
        if(maxScore < score){
            this.maxScore = score;
        }
        if(minScore > score){
            this.minScore = score;
        }
        this.totalScore += score;
        this.count++;
        this.endScore = score;
        this.updateTime = instant.toEpochMilli();
    }

    public void setRepoTime(long repoTime){
        this.repoTime = repoTime;
    }

    public int getPositionId() {
        return positionId;
    }

    public int getIndex() {
        return index;
    }

    public float getStartScore() {
        return startScore;
    }

    public float getEndScore() {
        return endScore;
    }

    public float getMaxScore() {
        return maxScore;
    }

    public float getMinScore() {
        return minScore;
    }

    public float getTotalScore() {
        return totalScore;
    }

    public int getCount() {
        return count;
    }

    public long getUpdateTime() {
        return updateTime;
    }

    public long getRepoTime() {
        return repoTime;
    }

    @Override
    public String toString() {
        return new StringJoiner(", ", MeterPoint.class.getSimpleName() + "[", "]")
                .add("positionId=" + positionId)
                .add("index=" + index)
                .add("startScore=" + startScore)
                .add("endScore=" + endScore)
                .add("maxScore=" + maxScore)
                .add("minScore=" + minScore)
                .add("totalScore=" + totalScore)
                .add("count=" + count)
                .add("updateTime=" + updateTime)
                .add("repoTime=" + repoTime)
                .toString();
    }
}
